package com.zzxx.wechat.demo;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

import com.zzxx.wechat.exception.BusinessException;
import org.apache.http.HttpHost;
import org.apache.http.auth.*;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;


class Client {
    public static final String username = "brd-customer-hl_b3246fe5-zone-residential_proxy1";
    public static final String password = "n62p45lthsqb";
    public static final int port = 33335;
    public String session_id = Integer.toString(new Random().nextInt(Integer.MAX_VALUE));
    public CloseableHttpClient client;

    public Client(String country) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        String login = username+(country!=null ? "-country-"+country : "")
            +"-session-" + session_id;
        HttpHost super_proxy = new HttpHost("brd.superproxy.io", port);
        CredentialsProvider cred_provider = new BasicCredentialsProvider();
        cred_provider.setCredentials(new AuthScope(super_proxy),
            new UsernamePasswordCredentials(login, password));
        // 信任所有证书
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (arg0, arg1) -> true).build();
        // 创建主机名验证器，用于绕过主机名验证
        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
        // 创建 SSL 连接套接字工厂，将自定义的 SSL 上下文和主机名验证器应用于 HTTPS 连接
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        client = HttpClients.custom()
         //   .setConnectionManager(new BasicHttpClientConnectionManager())
            .setProxy(super_proxy)
              // .setSSLSocketFactory(sslsf)
      .setDefaultCredentialsProvider(cred_provider)
            .build();


    }

    public String request(String url) throws IOException {
        String protocolAndDomain = queryProtocolAndDomain(url);
        HttpGet request = new HttpGet(url);
        request.setHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
        request.setHeader("Referer", protocolAndDomain);
        request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");

        CloseableHttpResponse response = client.execute(request);
        try {
            return EntityUtils.toString(response.getEntity());
        } finally { response.close(); }
    }
    private static String queryProtocolAndDomain(String url) {
        try {
            URL arcteryxUrl = new URL(url);
            return arcteryxUrl.getProtocol() + "://" + arcteryxUrl.getHost() + "/";
        } catch (MalformedURLException e) {
            throw new BusinessException("链接输入有误，请重新输入");
        }
    }
    public void close() throws IOException { client.close(); }
}

public class Example {
    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, InterruptedException {

    /*    Client client = new Client(null);
        try {
            // Put complete scraping sequence below:
            System.out.println(client.request("https://arcteryx.com/us/en/shop/X000009141"));
            // System.out.println(client.request(...second request...));
        } finally { client.close(); }*/


        Thread.sleep(5000);
    }
}